package net.neptech.config;

import net.neptech.model.dto.AppointmentDTO;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.support.serializer.JsonDeserializer;

import java.util.HashMap;
import java.util.Map;
@Configuration
public class DynamicDeserializer implements Deserializer<Object> {
    private Map<String, Deserializer<?>> topicDeserializers;

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {
        // 初始化主题与反序列化器的映射关系
        topicDeserializers = new HashMap<>();
        topicDeserializers.put("slot_use_update", new JsonDeserializer<>(AppointmentDTO.class));
//        topicDeserializers.put("log-topic", new StringDeserializer());
    }

    @Override
    public Object deserialize(String topic, byte[] data) {
        Deserializer<?> deserializer = topicDeserializers.get(topic);
        if (deserializer == null) {
            throw new IllegalArgumentException("Unsupported topic: " + topic);
        }
        return deserializer.deserialize(topic, data);
    }

    @Override
    public void close() {
        topicDeserializers.values().forEach(Deserializer::close);
    }
}

